package alo.p11.work;

/**
 * @ClassName PrintRepeatedElement
 * @Description 打印两个重复元素
 * @Author Fanxc
 * @DATE 2021/3/12 19:55
 * @Version 1.0
 */
public class PrintRepeatedElement {
    //使用异或
    //其他方法（双重循环、排序查找、散列法、求和求积法）
    void printRepeatedElement(int[] A,int n){
        int XOR=A[0];
        int right_most_set_bit_no,X=0,Y=0;
        for (int i = 1; i < n; i++) {
            XOR^=A[i];
        }
        for (int i = 1; i <= n; i++) {
            XOR^=i;
        }
        right_most_set_bit_no=XOR & (~( XOR-1));//求最右边的1
        for (int i = 0; i <n; i++) {
            if ((A[i]&right_most_set_bit_no)==right_most_set_bit_no){
                X=X^A[i];
            }else {
                Y=Y^A[i];
            }
        }
        for (int i = 1; i <= n; i++) {
            if ((A[i]&right_most_set_bit_no)==right_most_set_bit_no){
                X=X^i;
            }else {
                Y=Y^i;
            }
        }
        System.out.println("1："+X+"  2:"+Y);
    }
}
